package com.wanma.recuit.bosscontroller;

import com.alibaba.fastjson.JSON;
import com.wanma.recuit.pojo.*;
import com.wanma.recuit.service.AddCompanyInfoService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;

@Controller
public class AddCompanyInfoController {
    @Resource
    private AddCompanyInfoService addCompanyInfoService;

    //注册时第一页公司信息添加页面
    @GetMapping("/bossmanage/addcompanyinfo1")
    public  void  addCompanyInfo1(Model model){
        //把省份和城市查询出来
        List<City> cities = addCompanyInfoService.findProcinceCities();
        model.addAttribute("cities",cities);
        //把一级分类查出来
        List<Industry> industries = addCompanyInfoService.findIndustrys();
        model.addAttribute("industrys",industries);
        //查询企业规模
        List<Dictionary> dictionaries = addCompanyInfoService.findCompanyScale();
        model.addAttribute("dictionaries",dictionaries);

    }
    //有省份得到城市
    @PostMapping("/bossmanage/getcities")
    @ResponseBody
    public void getCities(Integer id, HttpServletResponse response) throws IOException {
        //把城市全部查出来
        List<City> cities = addCompanyInfoService.findCities(id);
        String s = JSON.toJSONString(cities);


        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.print(s);
        out.close();

    }
    //图片上传
    @RequestMapping("/bossmanage/uploadPic")
    @ResponseBody
    public String uploadPic(MultipartFile file, HttpServletRequest request){
        //得到文件原始的名字
        String  originalFileName = file.getOriginalFilename();
        //找到扩展名.jpg等
        String ext = FilenameUtils.getExtension(originalFileName);
        if (!",jpg,jpeg,bmp,gif,png,".contains("," + ext + ",")) {
            return null;
        }
        //唯一文件名
        String fileName = UUID.randomUUID() + "." + ext;
        //物理路径
        String realPath = request.getServletContext().getRealPath("/static/upLoadImages");

        File newfile = new File(realPath, fileName);
        try {
            file.transferTo(newfile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  fileName;
    }
    //向数据库添加公司的信息 其中第一页的信息及状态标志
    @PostMapping("/bossmanage/addCompanyInfo")
    @ResponseBody
    public void addCompanyInfo(Company company,HttpServletRequest request){
        company.setOnecompany(true);
        addCompanyInfoService.insertOneCompanyInfo(company);
        //将公司的id放入到公司所有者的表中

        HttpSession session = request.getSession();
        session.setAttribute("company",company);
        CompanyUser companyUser =(CompanyUser)session.getAttribute("companyUser");
        System.out.println(companyUser.getUserPhone());
        System.out.println(company.getId());
        companyUser.setCompanyId(company.getId());
        addCompanyInfoService.putCompanyId(companyUser);
        session.setAttribute("companyUser",companyUser);

    }
    //注册时第二页的添加信息
    @GetMapping("/bossmanage/addcompanyinfo2")
    public  void  addCompanyInfo2(){}

    //将第二页的信息加入company表 然后同时提交审核信息并向信息表插入一条提交记录
    @PostMapping("/bossmanage/addcheckinfo")
    public String addCheckInfo(String name,String legalperson,MultipartFile file, HttpServletRequest request){
        System.out.println("wodeeeeeeee");
        HttpSession session = request.getSession();
        Company company = (Company) session.getAttribute("company");
        CompanyUser companyUser = (CompanyUser)session.getAttribute("companyUser") ;
        String ori = file.getOriginalFilename();
        String ext = FilenameUtils.getExtension(ori);
        if (!",jpg,jpeg,bmp,gif,png,".contains("," + ext + ",")) {
            return null;
        }
        //唯一文件名
        String fileName = UUID.randomUUID() + "." + ext;
        //物理路径
        String realPath = request.getServletContext().getRealPath("/static/upLoadImages");

        File newfile = new File(realPath, fileName);
        try {
            file.transferTo(newfile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("这个公司信息"+company);
        company.setBusinessLicense(fileName);
        company.setLegalperson(legalperson);
        company.setTwocheckinfo(true);

         //更新数据库信息，同时改变提交状态
        addCompanyInfoService.updateTwoCheckIofo(company);
        //向审核表添加一条申请记录
        addCompanyInfoService.submitCheck(companyUser);
        session.setAttribute("company",company);
        System.out.println(company.getBusinessLicense());
        return  "/bossmanage/checkcompanyinfo";
    }
    //注册时第三页的信息
    @GetMapping("/bossmanage/checkcompanyinfo")
    public void checkCompanyInfo(){}
    //注册时失败的页面
    @GetMapping("/bossmanage/fail")
    public void fail(){}
    @RequestMapping("bossmanage/fail1")
    public String fail1(HttpServletRequest request){
        HttpSession session = request.getSession();
        Company company = (Company) session.getAttribute("company");
        CompanyUser companyUser = (CompanyUser)session.getAttribute("companyUser") ;
        company.setTwocheckinfo(false);
        company.setWaitcheck(false);
        addCompanyInfoService.updateTwoThree(company);
        return "/bossmanage/addcompanyinfo2";
    }
}
